<template>
  <div>
    <el-dialog
      :title="isAdd ? '添加角色' : '编辑角色'"
      :visible.sync="isShow"
      center
      :before-close="cancel"
    >
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item
          prop="rolename"
          label="角色名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" :label-width="formLabelWidth">
          <el-tree
            default-expand-all
            :data="menulist"
            show-checkbox
            node-key="id"
            ref="tree"
            :props="{
              children: 'children',
              label: 'title',
            }"
            check-strictly
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#409EFF"
            inactive-color="#DCDFE6"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="comfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { roleAdd, roleInfo, roleEdit } from "@/request/api";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      form: {
        rolename: "", //角色名称
        menus: "", //角色权限 id数组
        status: 1, //状态1正常2禁用
      },
      formLabelWidth: "120px",
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 3, max: 7, message: "长度在 3 到 7 个字符", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      menulist: "menu/getMenuList",
    }),
  },
  mounted() {
    if (this.menulist.length == 0) {
      this.getMenu();
    }
  },
  methods: {
    lookup(id) {
      roleInfo({
        id,
      }).then((res) => {
        this.form = res.list;
        this.form.id = id;
        this.$refs.tree.setCheckedKeys(
          this.form.menus ? this.form.menus.split(",") : []
        );
        console.log(res, "获取一条数据");
      });
    },
    ...mapActions({
      getRole: "role/changeRoleAction",
      getMenu: "menu/changeMenuAction",
    }),
    reset() {
      (this.form = {
        rolename: "",
        menus: "",
        status: 1,
      }),
        this.$refs.form.resetFields();
      this.$refs.tree.setCheckedKeys([]);
    },
    cancel() {
      this.$emit("cancel", false), this.reset();
    },
    comfirm() {
      this.$refs.form.validate((flag) => {
        console.log(flag);
        if (flag) {
          console.log(this.form);
          // console.log(this.$refs.tree.getCheckedKeys());
          this.form.menus = this.$refs.tree.getCheckedKeys()
            ? this.$refs.tree.getCheckedKeys().join(",")
            : "";
          if (this.isAdd) {
            roleAdd(this.form).then((res) => {
              console.log(res, "角色添加");
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getRole();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            roleEdit(this.form).then((res) => {
              console.log(res, "角色编辑");
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getRole();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          alert("验证失败");
        }
      });
    },
  },
};
</script>

<style lang="less"scoped>
</style>
